package edenmedical;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Point;


public class FrmPIN implements Listener{
	private Display display;
	private Shell shell;
	private String dispositivo;
	private Text txtPIN;
	private Button btnOK;
	private Button btnAnnulla;
	
	public FrmPIN(Display display,String dispositivo){
		this.dispositivo = dispositivo;
		this.display = display;
		shell = new Shell(this.display);
		shell.setMinimumSize(new Point(132, 110));
		GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);
		shell.setText("Inserimento PIN");		
		gridLayout.numColumns = 2;
		gridLayout.verticalSpacing = 15;
		gridLayout.marginHeight = 20;
		gridLayout.marginWidth = 20;
		
		Label lblPIN = new Label(shell,SWT.NONE);
		lblPIN.setText("Inserisci PIN dispositivo");
		lblPIN.setAlignment(SWT.CENTER);
		lblPIN.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
		
		txtPIN = new Text(shell,SWT.BORDER | SWT.PASSWORD);
		txtPIN.setTextLimit(6);
		
		try {
			FileInputStream file = new FileInputStream("data/" + dispositivo + ".txt");
			byte[] pin= new byte[8];
			file.read(pin);
			String pinString = new String(pin);
			txtPIN.setText(pinString);
    		}
		catch (Exception err) {
    		System.out.println("Errore: " + err);
    		}

		btnOK = new Button(shell,SWT.PUSH);
		btnOK.setText("OK");
		GridData gd_btnOK = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
		gd_btnOK.widthHint = 47;
		btnOK.setLayoutData(gd_btnOK);
		btnOK.addListener(SWT.Selection,this);
		
		btnAnnulla = new Button(shell,SWT.PUSH);
		GridData gd_btnAnnulla = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_btnAnnulla.widthHint = 77;
		btnAnnulla.setLayoutData(gd_btnAnnulla);
		btnAnnulla.setText("Annulla");
		btnAnnulla.addListener(SWT.Selection,this);
	}
	
	public void Show(){
		shell.open();
		shell.pack();
		Display display = Display.getCurrent();
		Monitor primary = display.getPrimaryMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		while( !shell.isDisposed())
		{
			if(!display.readAndDispatch())
				display.sleep();
		}
	}
	
	public void handleEvent(Event e)
	{		
		if (e.widget.equals(btnOK))
		{
			try {
					FileOutputStream file = new FileOutputStream("data/" + dispositivo + ".txt");
					PrintStream output = new PrintStream(file);
					output.println(txtPIN.getText());
					FrmMain frmMain = FrmMain.getInstance();
					if (dispositivo.equals("Bilancia")) 
						frmMain.setLblStatoBilancia("Non associato");
					if (dispositivo.equals("Pressione"))
						frmMain.setLblStatoPressione("Non associato");
					if (dispositivo.equals("Omron"))
						frmMain.setLblStatoOmron("Non associato");
					if (dispositivo.equals("Sat300"))
						frmMain.setLblStatoSat300("Non associato");
		    	}
			catch (IOException err) {
		    		System.out.println("Errore: " + err);
		    		System.exit(1);
		    	}
			finally {shell.dispose();}
		}
		if (e.widget.equals(btnAnnulla))
		{
			shell.dispose();
		}		
	}
}